package com.liqi.sms.utils;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TokenUtil {
    public static String getToken(String url, String username, String password) {
        Logger logger = LoggerFactory.getLogger(TokenUtil.class);
        JSONObject params = new JSONObject();
        params.put("username", username);
        params.put("password", password);


        try {
            String json = SmartHttp.sendPostJson(url, params.toString(), null);
            JSONObject jsonObject = JSONObject.parseObject(json);
            if (json == null || json.equals("")) {
                logger.info("获取响应失败，发送失败");
                return null;
            } else if (!"200".equals(jsonObject.get("code").toString())) {
                logger.info("获取token报错信息：" + jsonObject.get("msg"));
                return null;
            } else {
                String token=jsonObject.getJSONObject("data").getString("access_token");
                logger.info("获取token成功:"+token);
                return token;

            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.info("访问接口失败");

        }
        return null;
    }

    public static void main(String[] args) throws Exception {
        JSONObject params = new JSONObject();
        params.put("username", "admin");
        params.put("password", "admin123");

        String json = SmartHttp.sendPostJson("http://121.33.195.139:8849/auth/token", params.toString(), null);

        JSONObject jsonObject = JSONObject.parseObject(json);
        System.out.println(jsonObject.getJSONObject("data").getString("access_token"));

//        String data = jsonObject.get("data").toString();
//        System.out.println(JSONObject.parseObject(data).get("access_token").toString());
    }
}


